/*******************************************************************
 * Copyright (C) yel_hb-USTC, 2012                                
 *                                                                  
 * FILE NAME             :  median.cpp                                
 * PRINCIPAL AUTHOR      :  yel_hb                               
 * MODULE NAME           :  MEDIAN                             
 * LANGUAGE              :  C++                                      
 * TARGET ENVIRONMENT    :  windows                                   
 * DATE OF FIRST RELEASE :  2012/12/18                              
 * DESCRIPTION           :  implentaion the methods defined in 
 *							median.h, when add a value, calculate
 *							the median immediately.
 ******************************************************************/

/*
 * Revision log:
 *
 * Created by yel_hb,2012/12/18
 *
 */

#include <algorithm>
#include "median.h"
using namespace std;

Median::Median()
{
	vlist.clear();
	median = 0;
}

void Median::reset () 
{
	vlist.clear();
	median = 0;
}

void Median::add ( double value )
{
	vlist.push_back ( value );
}

double Median::get_median ()
{
	int m;
	sort ( vlist.begin(), vlist.end() );
	m = vlist.size();
	median = m % 2 == 0 ? ( vlist[m / 2] + vlist[m / 2 - 1] ) / 2 : vlist[m / 2];

	return median;
}

Median::~Median ()
{
	vlist.clear();
}
